package com.linktel.productionLine.controller;

import com.linktel.productionLine.common.BaseResponse;
import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.common.ResultUtils;
import com.linktel.productionLine.entity.dto.stepYieldOffset.StepYieldOffsetDTO;
import com.linktel.productionLine.entity.query.stepYieldOffset.AddStepYieldOffsetQuery;
import com.linktel.productionLine.entity.query.stepYieldOffset.EditStepYieldOffsetQuery;
import com.linktel.productionLine.exception.BusinessException;
import com.linktel.productionLine.service.StepYieldOffsetService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 *
 * @Author HuangJiaHui
 * @Date 2023/8/25 9:35
 */
@RestController
@RequestMapping("/step-yield-offset")
@AllArgsConstructor
public class StepYieldOffsetController {

    private StepYieldOffsetService stepYieldOffsetService;

    /**
     * 查询所有工序良率补偿
     *
     * @return 工序良率补偿列表
     */
    @GetMapping
    public BaseResponse<List<StepYieldOffsetDTO>> listStepYieldOffset() {
        return ResultUtils.success(stepYieldOffsetService.listStepYieldOffset());
    }

    /**
     * 新增工序良率补偿
     *
     * @param addStepYieldOffsetQuery 新增的工序良率补偿封装对象
     * @return 是否新增成功
     */
    @PostMapping
    public BaseResponse<Boolean> addStepYieldOffset(@RequestBody AddStepYieldOffsetQuery addStepYieldOffsetQuery) {
        if (addStepYieldOffsetQuery == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(stepYieldOffsetService.addStepYieldOffset(addStepYieldOffsetQuery));
    }

    /**
     * 删除工序良率补偿
     *
     * @param id 主键id
     * @return 是否删除成功
     */
    @DeleteMapping
    public BaseResponse<Boolean> deleteStepYieldOffset(Long currentUserId, Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(stepYieldOffsetService.deleteStepYieldOffset(currentUserId, id));
    }

    /**
     * 修改工序良率补偿
     *
     * @param editStepYieldOffsetQuery 要修改的工序良率补偿
     * @return 是否更新成功
     */
    @PutMapping
    public BaseResponse<Boolean> editStepYieldOffset(@RequestBody EditStepYieldOffsetQuery editStepYieldOffsetQuery) {
        if (editStepYieldOffsetQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(stepYieldOffsetService.editStepYieldOffset(editStepYieldOffsetQuery));
    }

}
